<template>
    <div>
        <h1>添加会员</h1>
<table class="table table-bordered">
<tbody>
    <tr>
        <td align="right" >会员卡号</td>
        <td><input type="text" v-model="member.MemberCode" disabled></td>
    </tr>
    <tr>
        <td align="right">手机号</td>
        <td><input type="text" v-model="member.Phone"  maxlength="11"></td>
    </tr>
    <tr>
        <td align="right">用户名</td>
        <td><input type="text" v-model="member.UserName" ></td>
    </tr>
    <tr>
        <td align="right">身份证号</td>
        <td><input type="text" v-model="member.Card" maxlength="18"></td>
    </tr>
    <tr>
        <td align="right">会员等级</td>
        <td>
            <select  v-model="member.LevelId" >
                <option value="">请选择</option>
                <option :value="item.Id" v-for="item in data.list">{{item.Name}}</option>
            </select>
        </td>
    </tr>
    <tr>
        <td align="right">生日</td>
        <td><input type="date" v-model="member.BirthDay" ></td>
    </tr>
    <tr>
        <td align="right">性别</td>
        <td>
            <input type="radio" :value="1" name="sex" v-model="member.sex" >男
            <input type="radio" :value="0" name="sex"  v-model="member.sex" >女
        </td>
    </tr>
    <tr>
        <td align="right">积分</td>
        <td>
            <input type="text" v-model="member.Integration" >
        </td>
    </tr>
    <tr>
        <td align="right">金额</td>
        <td>
            <input type="text"  v-model="member.Money" >
        </td>
    </tr>
    <tr>
        <td align="right"></td>
        <td>
            <input type="button" value="添加" class="btn btn-primary" @click="AddMember">
        </td>
    </tr>
</tbody>
</table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,reactive,onMounted} from 'vue'
import moment from 'moment';


onMounted(()=>{
    getLevel();
})


var num = Math.random().toString().substr(2, 5)
let member:any=reactive({
    "MemberCode": moment(new Date).format("YYYYMMDD") + num,
    "Phone": "",
    "UserName": "",
    "Card": "",
    "LevelId": "",
    "BirthDay": "",
    "Sex": 1,
    "Integration": "",
    "Money": ""
})
let data: any = reactive({
    list: []
})
const getLevel = () => {
    axios({
        method: 'get',
        url: '/api/Member/GetMemberLevels',

    })
        .then(res => {
            data.list=res.data
            console.log(res);

        })
        .catch(err => {
            console.log(err);

        })
}
const AddMember=()=>{

    if (member.Phone == "") {
        alert("手机号不能为空");
        return;
    }
    let reg = /^1[3-9]\d{9}$/
    if (!reg.test(member.Phone)) {
        alert("手机号格式不正确");
        return;
    }
    if (member.UserName == "") {
        alert("姓名不能为空");
        return;
    }
    if (member.Card == "") {
        alert("身份证号码不能为空");
        return;
    }
    let regCard = /^\d{17}[0-9xX]$/
    if (!regCard.test(member.Card)) {
        alert("身份证格式不正确");
        return;
    }
    if (member.LevelId == "") {
        alert("等级必选");
        return;
    }
    if (member.BirthDay == "") {
        alert("生日不能为空");
        return;
    }
    if (member.Integration == "") {
        alert("积分不能为空");
        return;
    }
    if (isNaN(member.Integration)) {
        alert("积分必须是数值");
        return;
    }
    if (member.Money == "") {
        alert("余额不能为空");
        return;
    }
    if (isNaN(member.Money)) {
        alert("余额只能为数字");
        return;
    }
member.Integrationj=parseInt(member.Integration);
    axios({
        method:'post',
        url:'/api/Member/AddMember',
        data:member
    })
    .then(res=>{
        if(res.data==-1){
            alert("身份证号重复");
        }
        else if(res.data>0){
            alert("添加成功")
        }
        else{
            alert("添加失败")
        }
    })
    .catch(err=>{
        console.log(err);
        
    })
}
</script>

<style scoped>

</style>